#include "EquipDetailDialog.h"
#include "../../../model/static/StaticItem.h"
#include "../../../model/proxy/ItemProxy.h"
#include "../../../net/NCDefines.h"
#include "../../../net/NetController.h"

EquipDetailDialog::EquipDetailDialog(void)
	:mName(NULL),mLevel(NULL),mType(NULL),mProperty(NULL),mPropertyTitle(NULL),
	mEquipBtn(NULL), mTakeOffBtn(NULL), mUpgradeBtn(NULL),mMeltBtn(NULL),
	mItemIcon(NULL),mEquipIcon(NULL)
{
}


EquipDetailDialog::~EquipDetailDialog(void)
{
	CC_SAFE_RELEASE(mName);
	CC_SAFE_RELEASE(mLevel);
	CC_SAFE_RELEASE(mType);
	CC_SAFE_RELEASE(mProperty);
	CC_SAFE_RELEASE(mPropertyTitle);
	CC_SAFE_RELEASE(mItemIcon);
    CC_SAFE_RELEASE(mEquipBtn);
	CC_SAFE_RELEASE(mTakeOffBtn);
	CC_SAFE_RELEASE(mUpgradeBtn);
	CC_SAFE_RELEASE(mMeltBtn);
}

bool EquipDetailDialog::init()
{
	return true;
}

SEL_MenuHandler EquipDetailDialog::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName )
{
	
	return NULL;
}

SEL_CCControlHandler EquipDetailDialog::onResolveCCBCCControlSelector( CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onEquipBtnClick", EquipDetailDialog::onEquipBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this,"onTakeOffBtnClick", EquipDetailDialog::onTakeOffBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onUpgradeBtnClick", EquipDetailDialog::onUpgradeBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onMeltBtnClick", EquipDetailDialog::onMeltBtnClick);
	return NULL;
}

bool EquipDetailDialog::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mName", CCLabelTTF *, this->mName);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mLevel", CCLabelTTF *, this->mLevel);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mType", CCLabelTTF *, this->mType);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mProperty", CCLabelTTF *, this->mProperty);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mPropertyTitle", CCLabelTTF *, this->mPropertyTitle);
    CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mEquipBtn", CCControlButton *, this->mEquipBtn);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mTakeOffBtn", CCControlButton *, this->mTakeOffBtn);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mUpgradeBtn", CCControlButton *, this->mUpgradeBtn);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mMeltBtn", CCControlButton *, this->mMeltBtn);
	return false;
}

void EquipDetailDialog::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
	mEquipIcon = EquipIcon::create(this);
	mEquipIcon->setPosition(ccp(32, 142));
	mEquipIcon->setScale(0.4);
	addChild(mEquipIcon);
}

void EquipDetailDialog::onEnter()
{
	ModelDialog::onEnter();
}

void EquipDetailDialog::onExit()
{
	ModelDialog::onExit();
}

void EquipDetailDialog::_onNotification( CCObject* pSender )
{
	
}

void EquipDetailDialog::refresh()
{
	EquipInfo* itemData = (EquipInfo*)mData;
	EquipStatic* itemInfo = StaticItem::getSingletonPtr()->getEquipInfo(itemData->id);
	mName->setString(itemInfo->name);
	mLevel->setString(fcs("%d", itemData->level));
	mPropertyTitle->setString(StaticItem::getSingletonPtr()->getPropertyTitle(itemData->id));
	mProperty->setString(fcs("%d/%d", itemData->atkMin, itemData->atkMax));
	mType->setString(StaticItem::getSingletonPtr()->getTypeTitle(itemData->id));
	mEquipIcon->setInfo(itemData->id, itemInfo->quality);
    bool isPutOn = ItemProxy::getSingletonPtr()->isEquipPutOn(itemData->index);
    if(!isPutOn)
        mTakeOffBtn->removeFromParentAndCleanup(true);
    else
        mEquipBtn->removeFromParentAndCleanup(true);
}

void EquipDetailDialog::onEquipBtnClick(CCObject * pSender, CCControlEvent pCCControlEvent)
{
    EquipInfo* itemData = (EquipInfo*)mData;
    NetController::getSingletonPtr()->loadEquipage(itemData->index);
    close();
    
    //test
//    ItemProxy::getSingletonPtr()->putonIndex = itemData->index;
//    ItemProxy::getSingletonPtr()->putOnEquip(itemData->index);
//    bool result = true;
//    Post_Net_Notification(kNCLoadEquipage, (CCObject*)&result);
}

void EquipDetailDialog::onTakeOffBtnClick(CCObject * pSender, CCControlEvent pCCControlEvent)
{
    EquipInfo* itemData = (EquipInfo*)mData;
    NetController::getSingletonPtr()->unloadEquipage(itemData->index);
    close();
    
    
    //test
//    ItemProxy::getSingletonPtr()->putonIndex = itemData->index;
//    ItemProxy::getSingletonPtr()->takeOffEquip(itemData->index);
//    bool result = true;
//    Post_Net_Notification(kNCUnloadEquipage, (CCObject*)&result);
}

void EquipDetailDialog::onUpgradeBtnClick(CCObject * pSender, CCControlEvent pCCControlEvent)
{
	EquipInfo* itemData = (EquipInfo*)mData;
	ItemProxy::getSingletonPtr()->setLastQiangHuaEquip(itemData);
	Framework::getSingletonPtr()->changeState("EquipMergeScene");
	close();
}

void EquipDetailDialog::onMeltBtnClick(CCObject * pSender, CCControlEvent pCCControlEvent)
{
	//close();
	EquipInfo* itemData = (EquipInfo*)mData;
	ItemProxy::getSingletonPtr()->mSelectList.clear();
	ItemProxy::getSingletonPtr()->mSelectList.push_back(itemData->index);
	Framework::getSingletonPtr()->popup("MeltOkDialog", mData);
    close();
}

void EquipDetailDialog::touchScreen()
{
	close();
}
